const fs = require('fs')
const co = require('co')
const $request = require('request');
const {
	$_ULOG,
} = require('../app-logger/logger');

module.exports = function(options) {
	let thisReq = this
	return co(function*() {
		// let login = yield loginPromise.call(thisReq)
		return yield api(options)
	})
}

function api(options) {
	let thisReq = this
	return new Promise((resolve, reject) => {
		// setHeaders(options)
		// setCookie(options)
		$_ULOG.call(thisReq, `---[ofbizRequest] 后台接口请求 ${options.baseUrl}${options.url} 请求开始 === 参数: ${JSON.stringify(options)}---`)
		$request(options, function(err, res, body) {
			if (err) {
				$_ULOG.call(thisReq, err, 'error')
				return reject(err)
			}
			$_ULOG.call(thisReq, `---[ofbizResponse] 后台接口返回 ${options.baseUrl}${options.url} 返回结果: ${JSON.stringify(body)}---`);
			return resolve({
				res,
				body,
			})
		});
	})
}

function loginPromise() {
	let thisReq = this
	return new Promise(function(resolve, reject) {
		let options = {
			baseUrl: 'http://192.168.1.3:7777/ReportWS',
			url: '/login/report/userLogin.do',
			method: 'POST',
			form: {
				username: 'admin',
				password: '123456',
				validateCode: 'lmua',
			},
		}
		setHeaders(options)
			// setCookie(options)
		$_ULOG.call(thisReq, `---[ofbizRequest] 后台接口请求 ${options.baseUrl}${options.url} 请求开始 === 参数: ${JSON.stringify(options)}---`)
		$request(options, function(err, res, body) {
			if (err) {
				$_ULOG.call(thisReq, err, 'error')
				return reject(err)
			}
			$_ULOG.call(thisReq, `---[ofbizResponse] 后台接口返回 ${options.baseUrl}${options.url} 返回结果: ${JSON.stringify(body)}---`);
			return resolve(body)
		});
	})
}

function setHeaders(options) {
	options.headers = {
		// Accept: 'text/plain, */*; q=0.01',
		// 'Accept-Encoding': 'gzip, deflate',
		// 'Accept-Language': 'zh-CN,zh;q=0.8',
		// Connection: 'keep-alive',
		// 'Content-Length': 48,
		// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
		Cookie: 'JSESSIONID=1A4DCB84CE81692CF28F5E462E518598; OUTFOX_SEARCH_USER_ID_NCOO=1920672670.5657625; UM_distinctid=15ab6a526da0-001cdeb71d4171-5e4f2b18-13c680-15ab6a526dc3b9; OFBiz.Visitor=177995; connect.sid=s%3AY2KNZTE-mZQLXjqrzlKErDnlNCq2UWlG.fTwnlqjddTRJX%2FGXpHSGPJqXf3M8RdwbaOsSx%2BT%2BJm0; CNZZDATA1256824629=844884980-1461038064-%7C1491448489',
		// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36X-Requested-With:XMLHttpRequest',
	}
	return options
}

function setCookie(options) {
	let cookieStr = 'JSESSIONID=1A4DCB84CE81692CF28F5E462E518598; OUTFOX_SEARCH_USER_ID_NCOO=1920672670.5657625; UM_distinctid=15ab6a526da0-001cdeb71d4171-5e4f2b18-13c680-15ab6a526dc3b9; OFBiz.Visitor=177995; connect.sid=s%3AY2KNZTE-mZQLXjqrzlKErDnlNCq2UWlG.fTwnlqjddTRJX%2FGXpHSGPJqXf3M8RdwbaOsSx%2BT%2BJm0; CNZZDATA1256824629=844884980-1461038064-%7C1491448489'
	let cookie = $request.cookie(cookieStr)
	options.jar = $request.jar().setCookie(cookie, `${options.baseUrl}${options.url}`);
	return options
}